<?php
require_once("../../../config.inc.php");
$isReloadable = false;
require_once("../../../app/_inc/include.php");
require_once("./_inc/layoutFunction.php");

$layout_alias = sxREQ('layout_alias');
$layout = sxREQ('layout');
if(sxREQ("reqtype") == "save"){
	$errMsg .= layoutVarIns();
}
sxRDef('variable', '');
sxRDef('domain', '');
sxRDef('length', '');
sxRDef('decimals', '');

$sSQL = "SELECT * FROM domain WHERE frequent = 'Y' OR domain = '".sxRAS('domain')."'";
$objSDAO->query($sSQL,"domain");
$aValDom = array(''=>'-- select domain');
while($row=$objSDAO->fetch('domain')) {
	$aValDom[$row['domain']] = $row['description'];
}

$aOpz = array("kMenu"=>'pro.lay', "isReloadable"=>$isReloadable);
echo(headerHTML($aOpz));
?>
<script language="JavaScript" type="text/javascript">
function objFrmSubm(rt){
	document.objFrm.reqtype.value = rt;
	document.objFrm.submit();
}
</script>  
<?php
echo(bodyHTML($aOpz));


echo getLOHeader($layout);

$aCmd = array("insMD"=>array("pag"=>"javascript:objFrmSubm('save')",
										"title"=>_t("save"),
										"text"=>_t("save")
										),
					"back"=>array()
					);
$sHtmlCmd = commandsHTML($aCmd);
echo $sHtmlCmd;	

echo getHtmlForm('objFrm');
echo getHtmlHidden('xsn',XSN);
echo getHtmlHidden('reqtype','save');
echo getHtmlHidden('layout');
echo getHtmlHidden('layout_alias');

if ($layout_alias=='W') {
	$loaDes = 'simple memory variable';
} elseif ($layout_alias=='X') {
	$loaDes = '1 dimension array memory variable';
} else {
	$loaDes = '???';
}
echo("<table class='tabl01' width=\"100%\" cellpadding=\"3\" cellspacing=\"2\">\n");
echo("<tr>" .
		"<td class=\"titl01\"colspan=\"2\">Layout variable $layout_alias - $loaDes</td>" .
	"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Name*</td>" .
			"<td class=\"text01\">".$layout_alias."_".
			getHtmlText('variable',32,32)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Domain</td>" .
			"<td class=\"text01\">" .
			getHtmlSelect('domain', $aValDom).
			"</td>\n</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Length</td>" .
			"<td class=\"text01\">" .
			getHtmlText('length',4,4)."</td>" .
		"</tr>\n");
echo("<tr>" .
			"<td class=\"labl01\">Decimals</td>" .
			"<td class=\"text01\">" .
			getHtmlText('decimals',4,4)."</td>" .
		"</tr>\n");
echo("</table>");
echo $sHtmlCmd;	
echo("</form>");
echo("</td>\n</tr>\n</table>\n");
echo(footerHTML());
exit();

 function layoutVarIns($redirOnEnd = true){
 	global $objSDAO;
	$errMsg = "";
	$_REQUEST["variable"] = strtolower($_REQUEST["variable"]);
	if(sxREQUEST("variable") == "")
		$errMsg .= "missing name<br>";
	if(sxREQUEST("domain") == "")
		$errMsg .= "missing domain<br>";
	if(preg_match("/[^a-z0-9_]/",sxREQUEST("variable")))
		$errMsg .= "name: a-z 0-9 _ only<br>";	
	if(ereg("[^0-9_]",sxREQUEST("length")))
		$errMsg .= "wrong length<br>";	
	if(ereg("[^0-9_]",sxREQUEST("decimals")))
		$errMsg .= "wrong decimals<br>";	
	
	//existence
	$sSQL = "SELECT count(*) as num FROM layout_variable " .
			" WHERE layout='".sxRAS("layout")."' " .
			" AND layout_alias='".sxRAS("layout_alias")."'" .
			" AND variable='".sxRAS("variable")."'";
	$objSDAO->query($sSQL,"test");
	$row = $objSDAO->fetch("test");
	if($row["num"] != 0)
		$errMsg .= "existing name<br>";
	if($errMsg != ""){return $errMsg;}	
	if (sxRAS("layout_alias") > 'R') {
		$sSQL = "INSERT IGNORE INTO layout_alias (layout,layout_alias,entity) " .
			" VALUES('".sxRAS("layout")."','".sxRAS("layout_alias")."','')";
		$objSDAO->exec($sSQL);
	}
	$sSQL = "INSERT INTO " .
			"layout_variable " .
			"(layout, " .
			"layout_alias, " .
			"variable, " .
			"length, " .
			"decimals, " .
			"domain)" .
			"VALUES('".sxRAS("layout")."', " .
			"'".sxRAS("layout_alias")."', " .
			"'".sxRAS("variable")."', " .
			intval(sxRAS("length")).", " .
			intval(sxRAS("decimals")).", " .
			"'".sxRAS("domain")."'" .
			")";	
	
	$objSDAO->query($sSQL,"ins");
	$url = $_SESSION[USEROBJ]->getHistory(0, true);
	serverRedir($url);			
	return 'abnormal end';
}
